﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.BusinessLogical.DAL;
using CarolLib;
using DispatchAssist.Model;

namespace DispatchAssist.BusinessLogical.BLL
{
   public class CommonCacheData
    {

        private static IDataProvider _dataProvider = new SqlDataProvider();

        private static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        public static CacheAlertRule CacheAR
        {
            get
            {
                CacheAlertRule alertRule = CacheHelper.Get<CacheAlertRule>("Cache_AlertRule");
                if (alertRule == null)
                {
                    try
                    {
                        alertRule = new CacheAlertRule(_dataProvider.GetAlertRuleByPara(null, -1));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "AlertRule");

                        CacheHelper.Insert("Cache_AlertRule", alertRule, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in GetAllAlertRule", e);
                        throw e;
                    }
                }
                return alertRule;
            }
        }



        public static CacheEarlyTime CacheET
        {
            get
            {
                CacheEarlyTime earlyTime = CacheHelper.Get<CacheEarlyTime>("Cache_EarlyTime");
                if (earlyTime == null)
                {
                    try
                    {
                        earlyTime = new CacheEarlyTime( _dataProvider.GetEarlyTimeByPara(null));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "EarlyWarning");

                        CacheHelper.Insert("Cache_EarlyTime", earlyTime, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in Get EarlyTime", e);
                        throw e;
                    }
                }
                return earlyTime;
            }

        }



        public static CacheSeatTaiNo CacheST
        {
            get
            {
                CacheSeatTaiNo seatTailNo = CacheHelper.Get<CacheSeatTaiNo>("Cache_Seat");
                if (seatTailNo == null)
                {
                    try
                    {
                        seatTailNo = new CacheSeatTaiNo(_dataProvider.GetSeatMonitorByPara(null, null));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "SeatMonitor");

                        CacheHelper.Insert("Cache_Seat", seatTailNo, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in GetSeatMonitor", e);
                        throw e;
                    }
                }
                return seatTailNo;

            }
        }

        public static CacheACARDevice CacheACARDevice
        {
            get
            {
                CacheACARDevice devices = CacheHelper.Get<CacheACARDevice>("Cache_AcarsDevice");
                if (devices == null)
                {
                    try
                    {
                        devices = new CacheACARDevice(_dataProvider.GetDeviceModel(null));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "ACARSDevice");

                        CacheHelper.Insert("Cache_AcarsDevice", devices, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in Get Acars Device", e);
                        throw e;
                    }
                }
                return devices;
            }
        }

        public static CacheTemplate CacheTemplate
        {
            get
            {
                CacheTemplate templates = CacheHelper.Get<CacheTemplate>("Cache_Template");
                if (templates == null)
                {
                    try
                    {
                        templates = new CacheTemplate(_dataProvider.GetMsgTemplate(null, -1, null));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "MsgTemplate");

                        CacheHelper.Insert("Cache_Template", templates, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in Get Templates", e);
                        throw e;
                    }
                }
                return templates;

            }
        }

        public static CacheSeatLogin CacheLonginSeatInfo
        {
            get
            {
                CacheSeatLogin seats = CacheHelper.Get<CacheSeatLogin>("Cache_LoginInfo");
                if (seats == null)
                {
                    try
                    {
                        seats = new CacheSeatLogin(_dataProvider.GetAllLoginUser(false));

                        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency
                        (System.Configuration.ConfigurationManager.AppSettings["CurrentDB"], "SeatLogin");

                        CacheHelper.Insert("Cache_LoginInfo", seats, dependency);
                    }
                    catch (Exception e)
                    {
                        Logger.Error("error in Get Seat Login", e);
                        throw e;
                    }
                }
                return seats;

            }
        }

        //public static void RefireshCacheSeatLogin()
        //{ 
         
        //}

       

    }
}
